package core.sceneManager.sceneSection.scene2DSection
{
	import core.MainManager;
	import core.sceneManager.IScene;
	import core.sceneManager.SceneManager;
	import core.sceneManager.sceneSection.ISceneSection;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import memory.MemoryRecovery;
	
	import utils.XmlTranslater;
	/**
	 * 二维场景创建
	 * 封装了一些创建场景的操作，供给SceneManager调用
	 */ 
	public class Scene2D extends EventDispatcher implements IScene
	{
		private var index:int=-1;
		private var dataBase:XML;
		public function Scene2D(target:IEventDispatcher=null)
		{
			super(target);
		}
		/**
		 * 创建场景模块
		 */ 
		public function createSections(xml:XML):void
		{
			if(xml==null || xml.Section==null || xml.Section.length==0)
			{
				this.dispatchEvent(new Event(Event.COMPLETE));             //如果XML不存在或者没有需要创建的模块,那么直接抛出创建完毕的事件
			}
			this.dataBase=xml;
			createSection();
		}
		private function createSection():void
		{
			index++;
			if(index==dataBase.Section.length())
			{
				this.dispatchEvent(new Event(Event.COMPLETE));
			}else
			{
				var sectionXml:XML=dataBase.Section[index];
				var type:String=sectionXml.@type;                  //模块的类型，肯定要了
				var sectionName:String=sectionXml.@sectionName;    //模块的名字，用来区别模块
				var action:String=sectionXml.@action;
				if(action!="delete")
				{
					var layer:int=handleLayer(sectionXml.@layer);      //模块的层次，只有2D模块
					var section:ISceneSection;
					var container:*;
					switch(type)
					{
						case "sb":section=new SwfOrBitmapSection();container=MainManager.getInstance().getContainer(layer);break;
						case "music":section=new SoundSection();container=MainManager.getInstance().getContainer(layer);break;
						case "flv":section=new FlvSection();container=MainManager.getInstance().getContainer(layer);break;
					}
					SceneManager.sections.addSection(type,sectionName,XmlTranslater.translateBoolean(sectionXml.@autoRefresh),section);//存储模块
					section.excute(sectionXml,container);
					section.start();
					handleWaitForComplete(sectionXml.@waitForComplete,section);
				}else
				{
					SceneManager.sections.deleteSection(type,sectionName);
				}
			}
		}
		//模块创建完毕
		private function on_section_created(e:Event):void
		{
			this.createSection();//继续创建
			MemoryRecovery.getInstance().gcFun(e.currentTarget,Event.COMPLETE,on_section_created);//保持完毕的行为只执行一次
		}
		//设置层次，默认层次为0
		private function handleLayer(layer:String):int
		{
			if(layer==null || layer=="" || layer=="1")
			{
				return 1;
			}
			return int(layer);
		}
		//设置模块的waitForComplete属性,无需等待的话，直接加载新模块，要等待的话就等待模块加载完毕
		private function handleWaitForComplete(waitForComplete:String,section:ISceneSection):void
		{
			if(waitForComplete=="" || waitForComplete==null || waitForComplete=="0")
			{
				createSection();
			}else
			{
				section.addEventListener(Event.COMPLETE,on_section_created);
			}
		}
		public function dispose():void
		{
			dataBase=null;
		}
		
		public function pause():void
		{
		}
		public function resume():void
		{
		}
		/////////////////////////////////////////////下面的可以不用看///////////////////////
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			super.addEventListener(type,listener,useCapture,priority,useWeakReference);
		}
		
		override public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			super.removeEventListener(type,listener,useCapture);
		}
		
	}
}